package cl.fbd.widget_examples.web.snippet

import scala.xml.{NodeSeq, Node, PCData, Text, Unparsed}

import net.liftweb.widgets.flot._
import net.liftweb.widgets.trees.treetable._
import net.liftweb.widgets.trees.model._
import cl.fbd.widget_examples.web.model.{TimeSerieMeasureModel, TimeSerie, Measure, Region}

import net.liftweb.util.Helpers._
import net.liftweb.http.SHtml
import net.liftweb.http.js.JsCmd

import net.liftweb.http.js.jquery.JqJsCmds.DisplayMessage // (0.10)
// import net.liftweb.http.js.JsCmds.DisplayMessage // (0.10)

/*
 */

class EstacionTTFlotAjax (val ttfa : TreeTableFlotAjax) {
  def render (xhtml: NodeSeq, timeserie : TimeSerie) = {
    bind ("timeserie", xhtml, "name" --> (timeserie.what + " / " + timeserie.where),  
                             "sum" --> timeserie.sum,
                             "flot" --> SHtml.ajaxCheckbox (false, new SelectTimeSerie (timeserie, ttfa).select)
                             )
  }
}

/*
 * 
 */

class SelectTimeSerie (val timeSerie : TimeSerie, ttfa : TreeTableFlotAjax)
{
  /**
   * inside AJAX call, when the user (un)check the "is visibled" time-serie check box 
   */
 
  def select (selected : Boolean /* 0.10 */ ) = {
    // val selected = (szSelected == "true")  // 0.9
    
    // update the map 
    ttfa.timeSeriesVisible.update (timeSerie, selected)
    
    // list of visibled TimeSerie in the flot map
    val visibled = ttfa.visibled
    
    // convert to flot compatible serie
    val flotSeries = for (ts <- visibled) yield ts.toFlotSerie
    
    // return JS commands to plot the graph
    JsFlot ("ph_graph", flotSeries.toList, Constants.options)
  }
}

/*
 * render a treetable of time series and measures
 */

class TreeTableFlotAjax 
{
  val model = TimeSerieMeasureModel.initModel  
  
  // indicates if the time serie is displayed in the flot graph
  var timeSeriesVisible : scala.collection.mutable.Map [TimeSerie, Boolean] =
    new scala.collection.mutable.HashMap [TimeSerie, Boolean] ()
  
  // initialy, none of the time series is visibled
  timeSeriesVisible = model.preorder.foldLeft (timeSeriesVisible) ((map, x) =>
      x match {
        case timeSerie : TimeSerie => (map + (timeSerie -> false))
        case _ => map 
      }
    )
  
  // list of visibled TimeSerie in the flot map
  def visibled = timeSeriesVisible.filter (pair => pair._2).map (pair => pair._1)
  
  def renderMeasure (xhtml: NodeSeq, measure : Measure) = {
    bind ("measure", xhtml, "mm" --> measure.value, 
                             "date" --> Constants.sdf.format (measure.when))
  }
  
  def renderRegion (xhtml: NodeSeq, region : Region) = {
    bind ("region", xhtml, "name" --> region.name)
  }
  
  /*
   *
   */

  def render (xhtml: NodeSeq) = {
    bind ("all", xhtml, 
            "treetable" --> net.liftweb.widgets.trees.treetable.TreeTable.render (
                                  model, 
                                  new TreeTableOptions,
                                  new EstacionTTFlotAjax (this).render, 
                                    renderMeasure,
                                    renderRegion
                                  ),
            "flot" --> Flot.render ("ph_graph", Nil, Constants.options)
         )
  }
}

